//
//  ZYAudientListModel.m
//  AudientList
//
//  Created by zhangweiwei on 16/6/15.
//  Copyright © 2016年 Erica. All rights reserved.
//

#import "ZYAudientListModel.h"
#import "ZYTagModel.h"
#import "KCCategory.h"
#import "KCAudioTool.h"

extern CGFloat const ZYAudientListGeneralizeCellHeight;
extern NSInteger  const ZYAudientListCellIntroFontSize;
extern CGFloat    const ZYAudientListCellExceptIntroHeight;

@interface ZYAudientListModel ()

@property (nonatomic, assign) CGSize introSize;

@property (nonatomic, assign) CGFloat openRowH;
@property (nonatomic, assign) CGFloat closeRowH;
@property (nonatomic, assign) CGFloat singleLineIntroHeight;

@end

@implementation ZYAudientListModel

- (CGFloat)singleLineIntroHeight
{
    if (!_singleLineIntroHeight) {
        _singleLineIntroHeight = [@" " sizeWithAttributes:@{NSFontAttributeName : [UIFont systemFontOfSize:ZYAudientListCellIntroFontSize]}].height;
    }
    return _singleLineIntroHeight;
}

- (CGSize)introSize
{
    if (CGSizeEqualToSize(_introSize, CGSizeZero)) {
        
        NSString *intro = [NSString stringWithFormat:@"简介：%@。[收起]", self.introduce];
        CGFloat maxW = [UIScreen mainScreen].bounds.size.width - 30;
        CGSize maxSize = CGSizeMake(maxW, MAXFLOAT);
        
        _introSize = [intro boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:ZYAudientListCellIntroFontSize]} context:nil].size;
        
    }
    return _introSize;
}


- (NSArray *)tagModels
{
    if (!_tagModels) {
        
        if (!self.tag.length) return nil;
        
        NSArray *tagStr = [self.tag componentsSeparatedByString:@" "];
        
        
        NSMutableArray *arrayM = @[].mutableCopy;
        
        ZYTagModel *tagModel = [ZYTagModel new];
        
        tagModel.text = [NSString stringWithFormat:@"%zd", self.age];
        
        if (self.sex == ZYSexTypeMale) {
            tagModel.color = KC_HEX_COLOR(0x67d3ea);
            tagModel.image = [UIImage imageNamed:@"male"];
        }else {
            tagModel.color = KC_RGB_COLOR(251,117,135);
            tagModel.image = [UIImage imageNamed:@"female"];
            
        }
        
        [arrayM addObject:tagModel];
        
        NSArray *colors = @[
                            KC_HEX_COLOR(0xdaa34a),
                            KC_HEX_COLOR(0xff855a),
                            KC_HEX_COLOR(0x64b3fc),
                            KC_HEX_COLOR(0xf8b00c),
                            KC_HEX_COLOR(0xf88ab8),
                            KC_HEX_COLOR(0xd4a860),
                            KC_HEX_COLOR(0xff855a)
                            ];
        
        for (int i = 0; i < tagStr.count; i++) {
            
            NSString *tagText = tagStr[i];
            
            ZYTagModel *tagModel = [ZYTagModel new];
            
            tagModel.text = tagText;
            if (i < colors.count) {
                
                tagModel.color = colors[i];
            }else {
                
                tagModel.color = [UIColor orangeColor];
            }
            
            [arrayM addObject:tagModel];
        }
        _tagModels = arrayM;
        
    }
    return _tagModels;
}

- (NSArray *)discountTagModels
{
    if (!_discountTagModels) {
        
        if (!self.label.length || !self.labelColor.length) return nil;
        
        NSArray *texts = [self.label componentsSeparatedByString:@"_"];
        NSArray *colors = [self.labelColor componentsSeparatedByString:@"_"];
        
        NSMutableArray *arrayM = @[].mutableCopy;
        
        
        for (int i = 0; i < texts.count; i++) {
            
            ZYTagModel *tagModel = [ZYTagModel new];
            
            NSString *tag = texts[i];
            if (!tag.length) continue;
            tagModel.text = tag;
            
            NSString *color = colors[i];
            if (!color.length) continue;
            color = [color substringFromIndex:0];
            
            tagModel.color = [UIColor kc_colorWithHexString:color];
            
            [arrayM addObject:tagModel];
        }
        _discountTagModels = arrayM;
    }
    return _discountTagModels;
}

- (void)setIntroduce:(NSString *)introduce
{
    _introduce = [introduce copy];
    
    NSString *intro = [NSString stringWithFormat:@"简介：%@。", introduce];
    
    NSMutableDictionary *attrDict = [NSMutableDictionary dictionary];
    
    attrDict[NSFontAttributeName] = [UIFont systemFontOfSize:ZYAudientListCellIntroFontSize];
    attrDict[NSForegroundColorAttributeName] = [UIColor lightGrayColor];
    
    NSMutableAttributedString *introAttrStr = [[NSMutableAttributedString alloc] initWithString:intro attributes:attrDict];
    
    self.detailIntroAttributedString = introAttrStr;
    
    
    if (self.introSize.height >= 2 * self.singleLineIntroHeight + 1) {
        
        self.singleLineIntro = NO;
        
        NSRange range = [intro rangeOfString:@"\n"];
        
        NSString *shortIntro = nil;
        NSInteger maxCount = 35;
        if (range.location == NSNotFound || range.location > maxCount) {// 没有换行或者大于两行
            
            if (intro.length >= maxCount) {
                
                shortIntro = [intro substringToIndex:maxCount];
            }else {
                shortIntro = intro;
            }
            
            
        }else {// 在两行内
            
            shortIntro = [intro substringToIndex:range.location];
            
        }
        
        
        NSMutableAttributedString *shortIntroAttrStr = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"%@...", shortIntro] attributes:attrDict];
        
        self.shortIntroAttributedString = shortIntroAttrStr;
        
    }else {
        self.singleLineIntro = YES;
        self.shortIntroAttributedString = self.detailIntroAttributedString;
        
    }
    
}

- (CGFloat)openRowH
{
    if (!_openRowH) {
        
        _openRowH = ZYAudientListCellExceptIntroHeight + self.introSize.height;
        
    }
    return _openRowH;
}

- (CGFloat)closeRowH
{
    if (!_closeRowH) {
        _closeRowH = ZYAudientListCellExceptIntroHeight + self.singleLineIntroHeight * 2;
        
    }
    return _closeRowH;
}

- (CGFloat)rowHeight
{
    if (self.type == 1) {
        
        return self.isOpen ? self.openRowH : self.closeRowH;
    }
    return ZYAudientListGeneralizeCellHeight;
}




+ (NSDictionary *)modelCustomPropertyMapper {
    return @{
                 @"normalPic"  : @"pic.normal",
                 @"originPic"  : @"pic.origin",
                 @"smallPic"  : @"pic.small",
                 @"bigPic"  : @"pic.big",
                 @"audioIntro"  : @"userIntroVoice.voiceIntro",
                 @"audioIntroSecond"  : @"userIntroVoice.total"
             };
}


@end
